using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Services;
using SheLife.Domain.Entities;

namespace SheLife.AdminApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class HealthRecordsController : ControllerBase
    {
        private readonly IHealthRecordService _healthRecordService;

        public HealthRecordsController(IHealthRecordService healthRecordService)
        {
            _healthRecordService = healthRecordService;
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetHealthRecordById(int id)
        {
            var healthRecord = await _healthRecordService.GetHealthRecordByIdAsync(id);
            if (healthRecord == null)
            {
                return NotFound();
            }
            return Ok(healthRecord);
        }

        [HttpGet("user/{userId}")]
        public async Task<IActionResult> GetHealthRecordsByUserId(int userId)
        {
            var healthRecords = await _healthRecordService.GetHealthRecordsByUserIdAsync(userId);
            return Ok(healthRecords);
        }

        [HttpGet("user/{userId}/type/{recordType}")]
        public async Task<IActionResult> GetHealthRecordsByUserIdAndType(int userId, string recordType)
        {
            var healthRecords = await _healthRecordService.GetHealthRecordsByUserIdAndTypeAsync(userId, recordType);
            return Ok(healthRecords);
        }

        [HttpGet("user/{userId}/daterange")]
        public async Task<IActionResult> GetHealthRecordsByUserIdAndDateRange(
            int userId, 
            [FromQuery] DateTime startDate, 
            [FromQuery] DateTime endDate)
        {
            var healthRecords = await _healthRecordService.GetHealthRecordsByUserIdAndDateRangeAsync(userId, startDate, endDate);
            return Ok(healthRecords);
        }

        [HttpPost]
        public async Task<IActionResult> CreateHealthRecord([FromBody] HealthRecord healthRecord)
        {
            var createdHealthRecord = await _healthRecordService.CreateHealthRecordAsync(healthRecord);
            return CreatedAtAction(nameof(GetHealthRecordById), new { id = createdHealthRecord.Id }, createdHealthRecord);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateHealthRecord(int id, [FromBody] HealthRecord healthRecord)
        {
            if (id != healthRecord.Id)
            {
                return BadRequest();
            }

            var updatedHealthRecord = await _healthRecordService.UpdateHealthRecordAsync(healthRecord);
            if (updatedHealthRecord == null)
            {
                return NotFound();
            }
            return Ok(updatedHealthRecord);
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteHealthRecord(int id)
        {
            var result = await _healthRecordService.DeleteHealthRecordAsync(id);
            if (!result)
            {
                return NotFound();
            }
            return NoContent();
        }
    }
}